package net.ib.mtalk.ui.start;

import java.util.List;

import net.ib.mtalk.R;
import net.ib.mtalk.base.BaseActivity;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.service.SelecterService;
import android.app.ActivityManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

public class AccountTabActivity extends TabActivity {
	
	private static final String TAG_PHONE			= "Phone";
	private static final String TAG_ID				= "ID";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_account_tab);
		final TabHost host = getTabHost();
		
		final Intent intentMtalkBuddy = new Intent(this, AccountPhoneActivity.class);
		intentMtalkBuddy.addCategory(Intent.CATEGORY_TAB);
		final Intent intentPhoneBuddy = new Intent(this, AccountIdActivity.class);
		intentPhoneBuddy.addCategory(Intent.CATEGORY_TAB);
		
		host.addTab(host.newTabSpec(TAG_PHONE)
				.setIndicator(buildIndicator(getString(R.string.phone)))
				.setContent(intentMtalkBuddy));
		host.addTab(host.newTabSpec(TAG_ID)
				.setIndicator(buildIndicator(getString(R.string.mtalk_id)))
				.setContent(intentPhoneBuddy));
		
		host.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_selector);
		host.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_selector);
	}
	
	private View buildIndicator(String text) {
        final TextView indicator = (TextView) getLayoutInflater().inflate(R.layout.tab_indicator_auth,
                getTabWidget(), false);
        indicator.setText(text);
        return indicator;
    }
	
	public void onDestroy()
	{
		super.onDestroy();
		new Handler().postDelayed(new Runnable(){
			public void run() 
			{
				ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
				List<ActivityManager.RunningTaskInfo> info = am.getRunningTasks(50);
				for(int i = 0 ; i < info.size() ; i++)
				{
					if(info.get(i).topActivity.getPackageName().equals(getPackageName()))
						return;
				}
				
				SelecterService.setMode(AccountTabActivity.this, SelecterService.Mode.MONO_SELECT);
			}}, 300);
	}
	
	public void onResume()
	{
		super.onResume();
		SelecterService.setMode(this, SelecterService.Mode.FULL_SELECT);
	}
	
}
